"""
author：fc
date：  2021/10/17
"""
#
# sklearn中决策树的使用
# 主要是判断哪一个类别对最后的影响较大

import pandas as pd
import numpy as np
from sklearn.tree import DecisionTreeClassifier as DTC

data=pd.read_excel("../files/数据建模/lesson.xlsx",engine="openpyxl")
x=data.iloc[:,1:5]
y=data.iloc[:,5:6]
for column in x: # 这种还是没有循环方便,我花时间在多次一举
    x[column][x[column] =="是"]=1
    x[column][x[column] =="多"]=1
    x[column][x[column]!=1]=-1
y["销量"][y["销量"]=="高"]=1
y["销量"][y["销量"]!=1]=-1

x=x.values.tolist() # dataframe转list
y=y.values.tolist()
dtc=DTC(criterion="entropy")
dtc.fit(x,y)

# 决策树可视化
from sklearn.tree import export_graphviz
from sklearn.externals.six import StringIO
with open("../files/数据建模/DTC.dot",'w') as fh:
    export_graphviz(dtc,feature_names=['shizhan','keshishu','chuxiao','ziliao'],out_file=fh) # 特征名称并可视化
test_data=np.array([[1,-1,-1,1]]) #数据预测
res=dtc.predict(test_data)
print(f"预测结果：{res}")